/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unc.pds.servlets;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.RemoteException;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import unc.pds.data.Consts;
import unc.pds.filemodel.AlbumRemote;
import unc.pds.filemodel.PhotoRemote;
import unc.pds.model.Model;
import unc.pds.model.Postable;
import unc.pds.model.UserRemote;
import unc.pds.service.ServiceBeanRemote;
import unc.pds.service.ServiceBeanRemoteHome;
import unc.pds.util.Arch;
import unc.pds.util.ArchImpl;

/**
 *
 * @author Mikhail
 */

public class AlbumServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    private void Delete(Model object) throws RemoteException {
        if (object instanceof PhotoRemote) {
            File f = new File("photos\\"+object.getKey()+"-mini");
            f.delete();
            f = new File("photos\\"+object.getKey()+"-full");
            f.delete();
            //object.dropMe();
        } else if (object instanceof AlbumRemote) {
            PhotoRemote photos[] = ((AlbumRemote)object).getPhotos();
            for (int i=0; i<photos.length; i++) {
                Delete(photos[i]);
            }
            //object.dropMe();
        }
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        try {
            ServiceBeanRemote service = ((ServiceBeanRemoteHome)(new InitialContext()).lookup("ejb/ServiceBean")).create((Long)request.getSession().getAttribute("sid"));
            if (request.getParameter("action").equals("delete")) {
                Model object = (Model)service.getByKey(new Long(Long.parseLong(request.getParameter("objid"))));
                Model parent = object.getParent();
                Delete(object);
                object.dropMe();
                if (parent instanceof Postable) {
                    response.sendRedirect("index.jsp?id=" + parent.getKey()+ "&view=albums");
                } else if (parent instanceof AlbumRemote) {
                    response.sendRedirect("index.jsp?id=" + parent.getKey());
                } else throw new Exception("Wrong object type");
            } else if (request.getParameter("action").equals("addalbum")) {
                Model object = (Model)service.getByKey(new Long(Long.parseLong(request.getParameter("objid"))));
                Arch doc = new ArchImpl("root", null);
                doc.addChild(new ArchImpl(Consts.ATTR__ALBUM_NAME, request.getParameter("name")));
                doc.addChild(new ArchImpl(Consts.ATTR__ALBUM_AUTHOR, service.getUser().getKey().toString()));
                ((Postable)object).addAlbum(doc);
                response.sendRedirect("index.jsp?id=" + object.getKey()+ "&view=albums");
            }
        } catch (Exception e) {
            out.println(e.getLocalizedMessage());
        } finally { 
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
